package simple.demo;

import java.util.concurrent.TimeUnit;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Session;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.broker.BrokerFactory;
import org.apache.activemq.broker.BrokerService;

public class Server {

	private BrokerService broker;
	private ConnectionFactory connFactory;
	private Connection conn;

	public void run(String [] args) throws Exception {
		initBroker();
		
		initConnection();
		
		Session session = conn.createSession(true, Session.AUTO_ACKNOWLEDGE);
		MessageConsumer consumer = session.createConsumer(session.createQueue("T"));

		for(int k=0; k<1000 * 1000; ++k) {
			System.out.println(consumer.receive());
			TimeUnit.SECONDS.sleep(1);
		}
		
		TimeUnit.MINUTES.sleep(60); 
	}

	void initConnection() throws JMSException {
		connFactory = new ActiveMQConnectionFactory("vm://x?create=false");
		conn = connFactory.createConnection();
		conn.start();
	}

	void initBroker() throws Exception {
		broker = BrokerFactory.createBroker("xbean:server.xml?validate=false");
		broker.start();
	}
	
	void shutdown() throws Exception {
		broker.stop();
	}

}
